//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		     /_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.

using UnityEngine;
using System.Collections;

public class ViNoGOExtensions {
	
	static public void StripGameObjectName( GameObject obj , string oldValue , string newValue ){
		obj.name = obj.name.Replace( oldValue , newValue );
	}

	/// <summary>
	/// Gets the name of the target with parent name.
	/// </summary>
	/// <returns>
	/// The target name.
	/// </returns>
	static public string GetNameAppendedParent( GameObject go ){
		System.Text.StringBuilder sb = new System.Text.StringBuilder();
		if( go.transform.parent != null ){
			sb.Append( go.transform.parent.name );
			sb.Append( "/" );
			sb.Append( go.name );
		}
		return sb.ToString();
	}			

	// GameObject obj is not destroyed but destroys the children of obj.
	static public void  DestroyChildren ( GameObject obj ){						
		if( obj != null ){
#if false			
			int childCount = obj.transform.GetChildCount();
			while( childCount > 0 ){
				GameObject child = obj.transform.GetChild( 0 ).gameObject;
				GameObject.Destroy( child );				
				
				childCount = obj.transform.GetChildCount();
			}
#else			
			for( int i=0;i<obj.transform.GetChildCount();i++){			
				GameObject child = obj.transform.GetChild( i ).gameObject;
				ViNoDebugger.Log( "Now , Destroy ..."+ child.name );
				GameObject.Destroy( child );
			}
#endif			
		}		
	}
	
	// Destroy "name" GameObject Children. name Object is not Destroyed.
	static public void FindAndDestroyChildren (  string name   ){						
		GameObject obj = GameObject.Find( name );
		if( obj != null ){
			DestroyChildren( obj );
		}
		else{
			Debug.LogWarning( "Object :" + name + " is not found." );
		}
	}
	
	// Destroy "name" GameObject Children. name Object is not Destroyed.
	static public void  DestroyImmediateChildren (  string name   ){						
		GameObject obj = GameObject.Find( name );
		if( obj != null ){
			int childCount = obj.transform.GetChildCount();
			while( childCount > 0 ){
				GameObject child = obj.transform.GetChild( 0 ).gameObject;
				GameObject.DestroyImmediate( child );				
				
				childCount = obj.transform.GetChildCount();
			}
		}
	}
	
	static public string GetNameInHierarchy ( GameObject obj  )
	{
		string path = obj.name;
		while (obj.transform.parent != null)
		{
			obj = obj.transform.parent.gameObject;
			path = obj.name + "/" + path;
		}
		return "\"" + path + "\"";
	}
	
	static public string GetNameInHierarchyUntilTargetStrAppear ( GameObject obj , string targetstr )
	{
		string path = obj.name;
		while (obj.transform.parent != null)
		{
			obj = obj.transform.parent.gameObject;
			if( obj.name.Equals( targetstr ) ){
				break;	
			}
			path = obj.name + "/" + path;
		}
		return "\"" + path + "\"";
	}
	
	static public GameObject FindAndSendMessage( string name , string msg ){
		GameObject obj =  GameObject.Find( name );
		if( obj != null ){
			obj.SendMessage( msg );
		}
		return obj;
	}
	
	static public void InitTransform( GameObject obj , GameObject parent ){
		if( parent != null ){
			obj.transform.parent = parent.transform;		
		}
		obj.transform.localPosition	= Vector3.zero;
		obj.transform.localScale	= Vector3.one;		
	}
	
	static public void SetTransform( string name , GameObject obj , GameObject parent , Vector3 pos , Vector3 scale ){
		obj.name = name;
		if( parent != null ){
			obj.transform.parent = parent.transform;
		}
		obj.transform.localPosition = pos;				
		obj.transform.localEulerAngles = Vector3.zero;		
		obj.transform.localScale = scale;					
	}

	static public void SetTransform( string name , GameObject obj , GameObject parent , Vector3 pos , Vector3 rot , Vector3 scale ){
		SetTransform( name , obj , parent , pos , scale );
		obj.transform.localEulerAngles = rot;
	}

	static public void SetTransform( GameObject obj , GameObject parent , Vector3 pos , Vector3 rot , Vector3 scale ){
		SetTransform( obj.name , obj , parent , pos , rot , scale );
	}
	
	static public GameObject InstantiateFromResource( string path ){
		GameObject obj = GameObject.Instantiate( 
			Resources.Load( path )  ,
			Vector3.zero , Quaternion.identity ) as GameObject; 
		
		return obj;
	}
	
	static public GameObject InstantiateFromResource( string path , GameObject parent ){
		Object loadedObj = Resources.Load( path );	
		if( loadedObj == null ){
			Debug.LogError( "Resources.Load Error ! path:" + path );	
			return null;
		}
		GameObject obj = GameObject.Instantiate(  loadedObj , Vector3.zero , Quaternion.identity ) as GameObject; 
		
		InitTransform( obj , parent );
		
		return obj;
	}
	
	static public void ToggleActiveRecursivelyObject( GameObject obj ){
		bool t = obj.activeInHierarchy ? false : true;
		obj.SetActive( t );	
	}

}
